
** Merge Data
* Should only bo called from master file as directories are specified there

clear

** Import data ****************************************************************
import delimited "$rawdatapath/HK/iat.csv", clear delimiter(",")

* Following Table 3.3 from Lane et al. (2007) for IAT Scoring Procedures

* Step 1: delete trials > 10,000 msec and trials from practice block

drop if latency > 10000
drop if blockindex == 0

*Step 2: delete subjects where > 10% of trials are < 300 msec

gen lessthan300 = 0
replace lessthan300 = 1 if latency < 300
egen id = group(username)
sort id

egen n_trials = count(id), by (id)

egen sum_lessthan300 = total(lessthan300), by(id)
drop if sum_lessthan300 > 0.1*n_trials

*Step 2.1: correcting for incorrect responses [total or individual stage mean??]
egen id2 = group(id)
sort id2

su id2, meanonly
local n = r(max)

forvalues i = 1/2 {
quietly forvalues j = 1/`n' {
su latency if blockindex == `i' & id2 == `j' & iscorrectflag == "true"
scalar mean_latency_resp_`i'_`j' = r(mean)
scalar sd_latency_resp_`i'_`j' = r(sd)
}
}

// Here you need to decide which version of replacement to use,i.e. run only one of the two loops below!

forvalues i = 1/2 {
forvalues j = 1/`n' {
replace latency = round(mean_latency_resp_`i'_`j' + 600) if id2 == `j' & blockindex == `i' & iscorrectflag == "false"
}
}

*forvalues i = 1/`r(max)' {
*replace latency = round(mean_latency_resp_`i'_`j' + 2*sd_latency_resp_`i'_`j') if id2 == `j' & blockindex == `i' & iscorrectflag == "false"
*}

*Step 3: compute standard deviation

quietly forvalues j = 1/`n' {
su latency if id2 == `j'
scalar sd_latency_`j' = r(sd)
}

*Step 4: compute mean latency

forvalues i = 1/2 {
quietly forvalues j = 1/`n' {
su latency if id2 == `j' & blockindex == `i'
scalar mean_latency_`i'_`j' = r(mean)
}
}

*Step 5: compute mean differences

forvalues j = 1/`n' {
scalar mean_diff_`j' = mean_latency_2_`j' - mean_latency_1_`j'
}

*Step 6: compute D-score

forvalues j = 1/`n' {
scalar d_`j' = mean_diff_`j' / sd_latency_`j'
}

*Step 7: Store
gen dscore = .

forvalues j = 1/`n' {
replace dscore = d_`j' if id2 == `j'
}

drop lessthan300 id id2 n_trials sum_lessthan300

split username, parse("-")
drop username
rename username1 test_username
drop username*
rename test_username username
order username, first
drop if username=="noriko"
destring username, replace

preserve
* remove duplicates and keep dscore for each user
keep username dscore
sort username
gen dup = username[_n-1]
drop if dup == username
drop dup
rename username participant_id
save "$cleandatapath/HK/iat_user_dscore.dta", replace
restore

*}
